package com.turing.post.report.impl;

import java.math.BigInteger;
import java.util.Date;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.report.util.MchTradErrLog;
import com.turing.post.report.util.MchTradErrLogDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;

@Stateless(mappedName = "report.impl.MchTradErrLogDaoImpl")
@Remote
@Local
public class MchTradErrLogDaoImpl implements MchTradErrLogDao {
	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	private static final String filterSql = " from MchTradErrLog t where "
			+ " (t.tranTime between to_date(?2,'yyyy-MM-dd hh24:mi:ss') and to_date(?3, 'yyyy-MM-dd hh24:mi:ss')) "
			+ " and (t.merchantNo is NULL or t.merchantNo like ?1)"
			+ " and (?4 is NULL or t.merchantNo like ?4) "
			+ " and (?5 is NULL or t.cardNo like ?5) ";

	@SuppressWarnings("unchecked")
	public List<MchTradErrLog> getMchTradErrLogByFilter(Integer page,
			String issueCompanyNo, String startTime, String endTime,
			String merchantNo, String cardNo) {
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		final String sql = "select t.* "
				+ filterSql + "  order by t.TranTime desc";
		Query query = _em.createNativeQuery(sql, MchTradErrLog.class);
		query.setFirstResult(startPosition);
		query.setMaxResults(maxResult);
		if (PCPSUtil.isEmpty(issueCompanyNo))
			query.setParameter(1, "%");
		else
			query.setParameter(1, issueCompanyNo + "%");
		query.setParameter(2, startTime);
		query.setParameter(3, endTime);
		if (PCPSUtil.isEmpty(merchantNo))
			query.setParameter(4, null);
		else
			query.setParameter(4, "%" + merchantNo + "%");
		if (PCPSUtil.isEmpty(cardNo))
			query.setParameter(5, null);
		else
			query.setParameter(5, "%" + cardNo + "%");
		// query.setParameter(6, startPosition);
		// query.setParameter(7,
		// startPosition.add(BigInteger.valueOf(maxResult))
		// .subtract(BigInteger.ONE));
		return query.getResultList();
	}

	public BigInteger getMchTradErrLogCountByFilter(String issueCompanyNo,
			String startTime, String endTime, String merchantNo, String cardNo) {
		final String sql = "select nvl(count(*),0) " + filterSql;
		Query query = _em.createNativeQuery(sql);
		if (PCPSUtil.isEmpty(issueCompanyNo))
			query.setParameter(1, "%");
		else
			query.setParameter(1, issueCompanyNo + "%");
		query.setParameter(2, startTime);
		query.setParameter(3, endTime);
		if (PCPSUtil.isEmpty(merchantNo))
			query.setParameter(4, null);
		else
			query.setParameter(4, "%" + merchantNo + "%");
		if (PCPSUtil.isEmpty(cardNo))
			query.setParameter(5, null);
		else
			query.setParameter(5, "%" + cardNo + "%");
		return new BigInteger(query.getSingleResult().toString());
	}

	public void insert(MchTradErrLog mtel) throws PCPSApplicationException {
		GeneralDao.insert(_em, mtel);
	}

	public MchTradErrLog getMchTradErrLogByLogId(String logId) {
		return _em.find(MchTradErrLog.class, logId);
	}

	@TransactionAttribute(javax.ejb.TransactionAttributeType.REQUIRES_NEW)
	public void removeExpired(Date tranTime) {
		final String sql = "delete from MchTradErrLog where tranTime < ?1";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, tranTime);
		query.executeUpdate();
	}
}